//
//  RxSwiftNetWorkingVC.swift
//  RXSwiftTest
//
//  Created by ZhangBo on 2019/4/14.
//  Copyright © 2019 ZhangBo. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa


class RxViewController5: UIViewController {

    let surStr = "https://www.douban.com/j/app/radio/channels"
    let disposeB = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(pushNewSearchVC))
        self.title = "RxSwiftNetWorkingVC"
        self.view.backgroundColor = UIColor.orange
        //        textResponse()
        //        textData()
        textJson()
    }


    @objc func pushNewSearchVC() {
        self.navigationController?.pushViewController(TestViewController(), animated: true)
    }

}

extension RxViewController5 {

    func textJson() {
        let url = URL(string: surStr)
        URLSession.shared.rx.json(url: url!).subscribe(onNext: { (jsonData) in
            print("******************************")
            print("jsonData = \(jsonData)\n")
        }, onError: { (error) in
            print("******************************")
            print(error)
        }).disposed(by: disposeB)
    }


    func textData() {
        let url = URL(string: surStr)

        URLSession.shared.rx.data(request: URLRequest(url: url!)).subscribe(onNext: { (data) in
            print("******************************")
            print("data = \(data)\n")

        }, onError: { (error) in
            print("******************************")
            print(error)
        }).disposed(by: disposeB)
    }

    func textResponse() {

        let url = URL(string: surStr)
        URLSession.shared.rx.response(request: URLRequest(url: url!)).subscribe(onNext: { (resp, data) in
            print("******************************")
            print("reponse = \(resp)\n")
            print("******************************")
            print("data = \(data)\n")

        }, onError: { (error) in
            print("******************************")
            print(error)
        }).disposed(by: disposeB)

    }
}
